<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="__STATIC__/js/jquery.js"></script>
    <script src="__STATIC__/layui/layui.js"></script>
    <script src="__STATIC__/bootstrap-table/js/bootstrap-table.js"></script>
    <script src="__STATIC__/bootstrap-table/js/bootstrap-table-zh-CN.min.js"></script>
    <script src="__STATIC__/bootstrap-table/js/dropdown.js"></script>
    <link rel="stylesheet" href="__STATIC__/bootstrap-table/css/bootstrap-table.min.css">
    <link rel="stylesheet" href="__STATIC__/bootstrap-table/css/bootstrap.css">
    <script type="text/javascript" src="__STATIC__/x-admin/js/xadmin.js"></script>
    <link rel="stylesheet" href="__STATIC__/x-admin/css/font.css">
    <link rel="stylesheet" href="__STATIC__/x-admin/css/xadmin.css">
</head>
<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="#">资源管理</a>
        <a href="#">体质项目</a>
        <a href="#">首页</a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="layui-row">
    <form class="layui-form layui-col-md12 x-so" onsubmit="return false">
        <div class="layui-input-inline layui-show-xs-block" >
            <select name="school_id" id="grade_id" style="width: 500px">
                <option value="">年级</option>
                {foreach $grade as $val}
                    <option value="{$val.id}">{$val.grade_title}</option>
                {/foreach}
            </select>
        </div>
        <div class="layui-input-inline layui-show-xs-block" >
            <select name="school_id" id="type_id" style="width: 500px">
                <option value="">类别</option>
                {foreach $info as $val}
                    <option value="{$val.id}">{$val.title}</option>
                {/foreach}
            </select>
        </div>
        <button class="layui-btn"  id="search"><i class="layui-icon">&#xe615;</i></button>
    </form>
</div>
<xblock>
    <button class="layui-btn" id="add"><i class="layui-icon"></i>添加</button>
</xblock>
<table id="list">

</table>
</body>
<script>
    $('#list').bootstrapTable({
        //获取行号
        language: 'zh-CN',
        todayHighlight: true,
        striped: true,//设置为 true 会有隔行变色效果
        method: 'post',//服务器数据的请求方式 'get' or 'post'
        url: "{:url('/constitution_project/list')}",//服务器数据的加载地址
        searchOnEnterKey:true,
        sortName:'ID',//排序字段
        sortOrder : 'desc',//降序
        sidePagination: "server",   //分页方式：client客户端分页，server服务端分页（*）
        pagination: 'false',//设置为 true 会在表格底部显示分页条
        pageSize: '15',//如果设置了分页，页面数据条数
        pageList:[15,30,50],
        smartDisplay:false,
        //得到查询的参数
        queryParams : function (params) {
            //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            var temp = {
                rows: params.limit,                         //页面大小
                page: (params.offset / params.limit) + 1,   //页码
                grade_id:$("#grade_id option:selected").val(),
                type_id:$("#type_id option:selected").val()
            };
            return temp;
        },
        columns: [
            {title:'请选择',align:'center',checkbox : true},
            {field: 'id', title: 'ID', align:'center'},
            {title: '年级', align:'center',formatter:grade},
            {title: '类别', align:'center',formatter:type},
            {field: 'year', title: '年份', align:'center'},
            {title: '性别', align:'center',formatter:sex},
            {title: '区间', align:'center',formatter:score_group},
            {field: 'score',title: '得分', align:'center'},
            {align:'center', title:'操作', formatter:operateFormatter}
        ]}
    );
    function operateFormatter(val,rows) {
        return [
            "<button type='button' onclick='edit("+rows.grade_id+','+rows.type_id+','+rows.year+','+rows.sex+")' class='btn btn-info btn-xs'>修改</button>&nbsp;&nbsp;",
            "<button type='button' onclick='del("+rows.grade_id+','+rows.type_id+','+rows.year+','+rows.sex+")' class='btn btn-danger btn-xs'>删除</button>"
        ].join('');
    }
    //年级
    function grade(val,rows){
        return rows.grade.grade_title
    }
    //类别
    function type(val,rows){
        if(rows.type){
            return rows.type.title
        }

    }
    //性别
    function sex(val,rows){
        return rows.sex == 1 ? '男' : '女';
    }
    //区间
    function score_group(val,rows) {
        return rows.low_score+'-'+rows.high_score;
    }
    //添加
    $("#add").click(function () {
        layer.open({
            title:"添加",
            type:2,
            area:['750px','650px'],
            content:"{:url('/constitution_project/add')}"
        })
    })
    //删除
    function del(grade_id,type_id,year,sex) {
        layer.confirm('你确定要删除吗',{btn:['确定','取消']},function(){
            $.ajax({
                url:"{:url('/constitution_project/delete')}",
                type:"post",
                data:{
                    grade_id:grade_id,
                    type_id:type_id,
                    year:year,
                    sex:sex
                },
                dataType:"json",
                beforeSend: function(obj){
                    index = layer.msg('删除中请稍后', {
                        icon: 16
                        ,shade: 0.01
                        ,time:1000000
                    });
                },
                success:function(res){
                    if(res.code){
                        layer.msg(res.msg,{icon:1,time:2000},function(){
                            $('#list').bootstrapTable(('refresh'));
                        });
                        return;
                    }else{
                        layer.msg(res.msg,{icon:5,time:2000});
                        return;
                    }
                }
            });
        });
    }
    //检索
    $("#search").click(function () {
        $("#list").bootstrapTable(('refresh'));
    })
    //编辑
    function edit(grade_id,type_id,year,sex) {
        layer.open({
            title:"修改",
            type:2,
            area:['750px','650px'],
            content:"{:url('/constitution_project/edit')}?grade_id="+grade_id+"&type_id="+type_id+"&year="+year+"&sex="+sex
        })
        //window.location.href = "{:url('/constitution_project/edit')}?grade_id="+grade_id+"&type_id="+type_id+"&year="+year+"&sex="+sex
    }
</script>
</html>